package com.example.bootcurriculumproject.testController;

import com.example.bootcurriculumproject.entity.Goods;
import com.example.bootcurriculumproject.service.GoodsService;
import com.example.bootcurriculumproject.util.RedisUtil;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.transaction.annotation.Transactional;

@SpringBootTest
@AutoConfigureMockMvc
@Transactional
public class testGoodsController {
    @Autowired
    private MockMvc mockMvc;
    @MockBean
    private RedisUtil redisUtil;
    @MockBean
    private GoodsService goodsService;
    private static final String URL = "/goods";
    @Test
    public void addOne() throws Exception {
        Goods goods = new Goods("aaaaaa","测试","测试","测试","测试GoodService的AddOne方法",1,"L",3,"bbcc");
        Mockito.when(goodsService.addOne(Mockito.any())).thenReturn(1);
       String body = "{\"id\":\"aaaaaa\",\"type\":测试,\"images\":测试,\"alt\":测试,\"name\":测试GoodService的AddOne方法,\"price\":1,\"size\":\"L,\"describes\":\"bbcc\"";
        String result =  this.mockMvc.perform(MockMvcRequestBuilders.post(URL+"one")
                        .accept(MediaType.APPLICATION_JSON_VALUE)
                        .content(body.getBytes("utf-8"))
                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                ).andExpect(MockMvcResultMatchers.status().isOk())//isOk()=>200  isCreated()=>201
                .andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
                .andDo(MockMvcResultHandlers.print())
                .andReturn().getResponse().getContentAsString();

    }
    @Test
    public void update() throws Exception {
        Mockito.when(goodsService.update((Goods) Mockito.any())).thenReturn(true);
        String body = "{\"id\":\"bbbbb\",\"type\":测试1,\"images\":测试1,\"alt\":测试1,\"name\":测试update方法,\"price\":1,\"size\":\"L,\"describes\":\"dddd\"";
        String result =  this.mockMvc.perform(MockMvcRequestBuilders.put(URL)
                        .accept(MediaType.APPLICATION_JSON_VALUE)
                        .content(body.getBytes("utf-8"))
                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                ).andExpect(MockMvcResultMatchers.status().isOk())//isOk()=>200  isCreated()=>201
                .andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
                .andDo(MockMvcResultHandlers.print())
                .andReturn().getResponse().getContentAsString();
    }
    @Test
    public void Delete() throws Exception {
        Mockito.when(goodsService.delete( Mockito.any())).thenReturn(true);
        String result =  this.mockMvc.perform(MockMvcRequestBuilders.delete(URL)
                        .accept(MediaType.APPLICATION_JSON_VALUE)
                        .param("no","11122233")
                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                ).andExpect(MockMvcResultMatchers.status().isOk())//isOk()=>200  isCreated()=>201
                .andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
                .andDo(MockMvcResultHandlers.print())
                .andReturn().getResponse().getContentAsString();
    }
    @Test
    public void submitOneGoods() throws Exception {
        Mockito.when(goodsService.submitOneGoods(Mockito.any())).thenReturn(true);
        String body = "{\"addressnum\":\"ccccca\",\"uid\":测试2,\"consignee\":测试2,\"region\":测试2,\"address\":测试submitOneGoods方法,\"postalcode\":1";
        String result =  this.mockMvc.perform(MockMvcRequestBuilders.post(URL+"submitOne")
                        .accept(MediaType.APPLICATION_JSON_VALUE)
                        .content(body.getBytes("utf-8"))
                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                ).andExpect(MockMvcResultMatchers.status().isOk())//isOk()=>200  isCreated()=>201
                .andExpect(MockMvcResultMatchers.content().contentType("application/json;charset=UTF-8"))
                .andDo(MockMvcResultHandlers.print())
                .andReturn().getResponse().getContentAsString();
    }

}
